package com.baitengsoft.patentservicemonitor.job.cloud.download;

import com.alibaba.fastjson.JSON;
import com.baitengsoft.patentservicemonitor.api.service.WebPlatformService;
import com.baitengsoft.patentservicemonitor.common.utils.DateUtils;
import com.baitengsoft.patentservicemonitor.job.model.PacPatentUpdateModel;
import com.baitengsoft.patentservicemonitor.job.model.PacPatentinfoModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author wangze
 * @date 2018-02-13 14:44.
 **/
@Component
public class InitAliYunUpdateDataService extends AbstractInitDataService {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WebPlatformService webPlatformService;

    @Override
    public String getFolder(int platformId) {
        return "data-updated/" + platformId + "/" + DateUtils.getYear(new Date()) + "/" + String.valueOf(DateUtils.getMonth(new Date()));
    }

    @Override
    protected PacPatentinfoModel dealData(Integer pid, String json, String key) {
        PacPatentUpdateModel p = JSON.parseObject(json, PacPatentUpdateModel.class);
        if (p == null) {
            log.warn("获取oss专利失败，专利名称为:" + getName(key));
            return null;
        }
        p.setPlatformid(pid);
        List<String> storedPatents = pacPatentinfoService.queryBatch(new String[]{p.getAn()});
        PacPatentinfoModel model = p.Convert();
        //存在
        if (storedPatents != null && storedPatents.size() == 1) {
            p.setId(Integer.valueOf(storedPatents.get(0)));
            pacPatentinfoService.updateByBatch(Arrays.asList(model));
        } else {
            pacPatentinfoService.insertByBatch(Arrays.asList(model));
        }
        System.out.println(model.getAn());
        return model;

    }

    @Override
    protected boolean getIsRunRpt() {
        return true;
    }

    @Override
    protected int getMaxFetchDataSize() {
        return 100;
    }

    @Override
    public void batchInsert(Integer pid) {

    }

    @Override
    public void fetchDataSuccessCallback(Integer pid) {
        webPlatformService.updatePatentNum(pid);
    }
}
